package cn.web.management.web.servlet.inform.user;

import cn.web.management.domain.PersonalInform;
import cn.web.management.service.general.EditPersonalInform;
import cn.web.management.service.impl.FindEmployImpl;
import cn.web.management.service.inface.FindEmploy;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet(name = "editPersonalInformServlet", value = "/editPersonalInformServlet")
public class editPersonalInformServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @description: 用户编辑个人信息后更新数据库
     * @paramName: [request, response]
     * @paramType: [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @returnType: void
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        FindEmploy findInform = new FindEmployImpl();
        HttpSession session = request.getSession();

        Map<String, Object> employno = findInform.findEmploy((String) session.getAttribute("employno"));
        Map<String, String[]> parameterMap = request.getParameterMap();

        PersonalInform p = new PersonalInform();
        try {
            BeanUtils.populate(p,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        EditPersonalInform editPersonalInform = new EditPersonalInform();
        boolean b = editPersonalInform.editInform(employno,p);
        response.getWriter().write(b+"");
    }
}
